/* model */
import { BaseSelectUserPropsOptions } from '@src/component/common/BaseSelectUser/model'
/* hooks */
import { useFetchMarkList } from '@src/component/common/BaseSelectUser/hooks/tab'
import { useFetchStateList, useFetchLabelList } from '@src/component/common/BaseSelectUser/hooks/user'
import { 
  useFetchServiceProviderListAndServiceProviderUserList,
  useFetchServiceProviderQualificationList, 
  useFetchServiceProviderTypeList, 
} from '@src/component/common/BaseSelectUser/hooks/serviceProvider'
import { useFetchRoleListAndRoleUserList } from '@src/component/common/BaseSelectUser/hooks/role'
import { useFetchDepartmentListAndDepartmentUserList } from '@src/component/common/BaseSelectUser/hooks/department'
/* store */
import {
  useBaseSelectUserStoreCurrentTabType,
  useBaseSelectUserStoreDepartmentData,
  useBaseSelectUserStoreMarkList,
} from '@src/component/common/BaseSelectUser/store'
/* util */
import { isMulti as isBaseSelectUserMulti } from '@src/component/common/BaseSelectUser/util'
import Log from '@src/util/log'
import { isDepartmentTabByTabType, isRoleTabByTabType, isServiceProviderTabByTabType } from '@src/component/common/BaseSelectUser/util'
/* vue */
import { computed, onBeforeMount } from 'vue'

function useBaseSelectUser(options: BaseSelectUserPropsOptions) {
  
  const { markList } = useBaseSelectUserStoreMarkList()
  const { departmentList } = useBaseSelectUserStoreDepartmentData()
  
  const isMulti = computed(() => isBaseSelectUserMulti(options))
  
  const initialize = useBaseSelectUserInitialize()
  
  onBeforeMount(() => {
    initialize()
  })
  
  return {
    isMulti,
    markList,
    departmentList
  }
  
}

/** 
 * @description 选人组件初始化
*/
function useBaseSelectUserInitialize() {
  
  const fetchMarkList = useFetchMarkList()
  const fetchStateList = useFetchStateList()
  const fetchLabelList = useFetchLabelList()
  
  const { currentTabType } = useBaseSelectUserStoreCurrentTabType()
  
  // 获取部门和部门人员列表
  const fetchDepartmentListAndDepartmentUserList = useFetchDepartmentListAndDepartmentUserList()
  
  // 获取角色和角色人员列表
  const fetchRoleListAndRoleUserList = useFetchRoleListAndRoleUserList()
  
  // 获取服务商和服务商人员列表
  const fetchServiceProviderListAndServiceProviderUserList = useFetchServiceProviderListAndServiceProviderUserList()
  // 获取服务商资质列表
  const fetchServiceProviderQualificationList = useFetchServiceProviderQualificationList()
  // 获取服务商类型列表
  const fetchServiceProviderTypeList = useFetchServiceProviderTypeList()
  
  const initialize = async () => {
    try {
      
      // 首先 获取选项卡标签列表
      await fetchMarkList()
      
      if (isDepartmentTabByTabType(currentTabType.value)) {
        fetchDepartmentListAndDepartmentUserList()
      }
      
      if (isRoleTabByTabType(currentTabType.value)) {
        fetchRoleListAndRoleUserList()
      }
      
      if (isServiceProviderTabByTabType(currentTabType.value)) {
        fetchServiceProviderListAndServiceProviderUserList()
        fetchServiceProviderQualificationList()
        fetchServiceProviderTypeList()
      }
      
      // 获取用户状态列表
      fetchStateList()
      
      // 获取人员标签列表
      fetchLabelList()
      
    } catch (error) {
      Log.error(error, useBaseSelectUserInitialize.name)
    }
  }
  
  return initialize
}

export * from '@src/component/common/BaseSelectUser/hooks/department'
export * from '@src/component/common/BaseSelectUser/hooks/filter'
export * from '@src/component/common/BaseSelectUser/hooks/nav'
export * from '@src/component/common/BaseSelectUser/hooks/options'
export * from '@src/component/common/BaseSelectUser/hooks/params'
export * from '@src/component/common/BaseSelectUser/hooks/role'
export * from '@src/component/common/BaseSelectUser/hooks/search'
export * from '@src/component/common/BaseSelectUser/hooks/selected'
export * from '@src/component/common/BaseSelectUser/hooks/serviceProvider'
export * from '@src/component/common/BaseSelectUser/hooks/tab'
export * from '@src/component/common/BaseSelectUser/hooks/user'
export * from '@src/component/common/BaseSelectUser/hooks/projectTeam'

export {
  useBaseSelectUser,
  useBaseSelectUserInitialize
}
